package com.iweb.service;

import com.common.datasource.DataSourceUtil;
import com.common.model.DataGrid;
import com.common.model.Page;
import com.iweb.dao.AdminDao;
import com.iweb.dao.AdminDaoImpl;
import com.iweb.pojo.Admin;

import java.util.List;
import java.util.UUID;

public class AdminService {

    public Admin findUserByUsername(String username) {
        Admin admin = null;
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        AdminDao adminDao = new AdminDaoImpl(dataSourceUtil.getConnection());

        try {
            // 业务逻辑
            admin = adminDao.findUserByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dataSourceUtil.close();
        }
        return admin;
    }

    public DataGrid<Admin> findAdminList(Page page) {
        DataGrid<Admin> dataGrid = new DataGrid<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        AdminDao adminDao = new AdminDaoImpl(dataSourceUtil.getConnection());

        try {

            long count =adminDao.selectCount();
            List<Admin> list = adminDao.findAll(page);
            dataGrid.setCode(0);
            dataGrid.setMsg("");
            dataGrid.setCount(count);
            dataGrid.setData(list);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return dataGrid;
    }

    // 方法同步的
    public synchronized boolean addAdmin(Admin admin) {

        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        AdminDao adminDao = new AdminDaoImpl(dataSourceUtil.getConnection());

        try {
            Admin user = adminDao.findUserByUsername(admin.getUsername());
            // 验证用户是否可以注册
            if(user != null){
                throw new RuntimeException();
            }
            // 注册用户
            // 设定主键
            admin.setAid(UUID.randomUUID().toString());
            adminDao.save(admin);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            dataSourceUtil.close();
        }
    }

    public void deleteById(String aid) {
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        AdminDao adminDao = new AdminDaoImpl(dataSourceUtil.getConnection());

        try {
            adminDao.deleteById(aid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
    }

    public Admin findById(String aid) {
        Admin admin = null;
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        AdminDao adminDao = new AdminDaoImpl(dataSourceUtil.getConnection());

        try {
            admin = adminDao.findById(aid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return admin;
    }
}
